import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import Vant from 'vant';
import Axios from 'axios'
import qs from 'qs';
import 'vant/lib/index.css';

//复制
import VueClipboard from 'vue-clipboard2'
Vue.use(VueClipboard)

Vue.use(Vant);

Vue.config.productionTip = false


Vue.prototype.$axios = Axios;
const isPro = Object.is(process.env.NODE_ENV, "production"); // 判断开发环境，true为生产环境即线上，false为开发环境
isPro ? Axios.defaults.baseURL = "/apiDb" : Axios.defaults.baseURL = "/apiDb/apiDb";//调试
// Axios.defaults.baseURL = "/apiDb/apiDb";//调试
// Vue.prototype.$urls = "http://47.92.154.247:8081/"; //线上url接口
Vue.prototype.$urls = "https://erpdb.ebaoku.com/"; //线上url接口

import {
  Button, Image, Toast, NavBar, Lazyload, Dialog, Form, Icon, Cell, CellGroup, PullRefresh, Checkbox, Search,
  Sticky, Field, Empty, Swipe, SwipeItem, Tab, Tabs, Tabbar, TabbarItem, Popup, Loading, List, Card, SwipeCell,
  ActionSheet, Uploader, Overlay, CountDown, GoodsAction, GoodsActionIcon, GoodsActionButton, Stepper, SubmitBar,
  Radio, RadioGroup, Picker
} from 'vant'

Vue.use(Button).use(Image).use(Toast).use(NavBar).use(Lazyload).use(Dialog).use(Sticky).use(Popup).use(Checkbox);
Vue.use(Form).use(Field).use(Icon).use(Cell).use(CellGroup).use(Empty).use(Swipe).use(SwipeItem).use(PullRefresh);
Vue.use(Tab).use(Tabs).use(Tabbar).use(TabbarItem).use(Loading).use(List).use(Overlay).use(ActionSheet).use(Uploader);
Vue.use(Card).use(CountDown).use(GoodsAction).use(GoodsActionButton).use(GoodsActionIcon).use(Stepper).use(SubmitBar);
Vue.use(SwipeCell).use(Radio).use(RadioGroup).use(Search).use(Picker); 

// http 请求拦截器
Axios.interceptors.request.use((config) => {
	var token = localStorage.getItem('token');
	if (token) {
		config.headers['Authorization'] =token;
	}
	if(config.method == 'post'){//query传参
		if(config.url!='/delivery/base/scanOrder'){
			config.params = config.data;
		}
		
	}
	// config.data=qs.stringify(config.data)//转为formdata数据格式
	console.log("config",config)
	return config
}, (err) => {
	return Promise.reject(err);
})

// http 响应拦截器
Axios.interceptors.response.use((response) => {
	var token = response.headers.authorization;
	if (token) {
		localStorage.setItem('token', token);
	}
	// var code = response.data.code;
	if(response.data.code!=1){
		Toast(response.data.msg)
	}
	return response.data
}, error => {
	console.log("错误接口信息",error.response)
	if (error.response) {
		Toast.clear();
		switch (error.response.status) {
			case 401:
				Dialog.alert({
					title: '登录提示',
					message: '登录信息已失效，请重新登录',
				}).then(() => {
					console.log("登录",router.currentRoute.fullPath)
					
					let redirect_uri = encodeURIComponent("https://erpdb.ebaoku.com/#/login")
					let appid="wx7bd487eb9a590326"
					
					window.location.href =
						"https://open.weixin.qq.com/connect/oauth2/authorize?appid="+appid+"&redirect_uri=" + redirect_uri +
						"&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect"
					
					
					// router.replace({
					// 	path: '/login',
					// 	query: {
					// 		redirect: router.currentRoute.fullPath
					// 	}
					// })
					// router.push({
					// 	path: '/login',
					// 	query: {
					// 		redirect: router.currentRoute.fullPath
					// 	}
					// })
				});
				break;
			case 422:
				Toast(error.response.message)
				break;
		}
	}

	return Promise.reject(error)
})

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')
